<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children'}"
    >
      <el-table-column prop="id" label="分类编号" sortable width="180"></el-table-column>
      <el-table-column prop="catename" label="分类名称" width="180"></el-table-column>
      <el-table-column label="图片" width="180">
        <template v-slot="scope">
          <img :src="$pregImg+scope.row.img" alt="" v-if='scope.row.img!=""'>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success">启用</el-button>
          <el-button v-else type="danger">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" sortable width="180">
        <template v-slot="scope">
          <el-button type="success" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// import { cateList } from "../../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  methods: {
    ...mapActions({
      requestCates: "cate/CateListActions",
      editCate: "cate/editActions",
      delcate: "cate/delActions"
    }),
    edit(id) {
      this.editCate(id), this.$emit("edit");
    },
    del(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 发起删除请求
          this.delcate(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      this.requestCates();
    }
  },
  mounted() {
    // this.requestCateList();
    this.requestCates();
  },
  computed: {
    ...mapGetters({
      tableData: "cate/tableData"
    })
  }
};
</script>

<style scoped>
img {
  width: 50px;
  height: 50px;
}
</style>
